package com.zkdcloud.controller;

import com.xdja.interceptor.annotation.BeforeProcess;
import com.xdja.interceptor.bean.ResultBean;
import com.xdja.interceptor.exception.InvokeException;
import com.xdja.interceptor.util.ValidateUtils;
import com.zkdcloud.aspectInterceptor.advice.PageAdvice;
import com.zkdcloud.aspectInterceptor.advice.TokenAdvice;
import com.zkdcloud.domain.Admin;
import com.zkdcloud.domain.Order;
import com.zkdcloud.dto.OrderDisplayBean;
import com.zkdcloud.dto.Pagination;
import com.zkdcloud.dto.QueryOrderBean;
import com.zkdcloud.service.OrderService;
import org.jfaster.mango.plugin.page.Page;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

import static com.zkdcloud.constant.Constants.HTTP_CONSTANTS.HTTP_SUCCESS;
import static com.zkdcloud.constant.Constants.HTTP_CONSTANTS.OPERATOR_SUCCESS;
import static com.zkdcloud.constant.Constants.HTTP_CONSTANTS.REQUEST_ADMIN;

/**
 * 订单controller管理
 *
 * @author zk
 * @since 2018/4/23
 */
@RestController
@RequestMapping(value = "/mg/order")
public class OrderController {
    /**
     * static logger
     */
    private static Logger logger = LoggerFactory.getLogger(OrderController.class);

    @Autowired
    private OrderService orderService;

    /**
     * 获取所有订单列表
     *
     * @param page 分页
     * @return resultBean
     */
    @BeforeProcess(advice = {PageAdvice.class, TokenAdvice.class})
    @RequestMapping(value = "/all",method = RequestMethod.GET)
    public ResultBean getList(Page page, QueryOrderBean queryOrderBean, Map<String,Object> map) throws InvokeException {
        Admin admin = (Admin) map.get(REQUEST_ADMIN);
        queryOrderBean.setBusinessId(admin.getBusinessId());
        List<OrderDisplayBean> orderDisplayBeans = orderService.getDisplayOrderListByBid(queryOrderBean,page);
        return ResultBean.successResult("请求成功",new Pagination(page,orderDisplayBeans));
    }

    /**
     * 获取订单详情
     *
     * @param id id
     * @return resultBean
     */
    @RequestMapping(value = "/detail/{id}",method = RequestMethod.GET)
    public ResultBean getDetail(@PathVariable("id")String id){
        OrderDisplayBean orderDisplayBean = orderService.getDisplayOrderOne(id);
        return ResultBean.successResult(HTTP_SUCCESS,orderDisplayBean);
    }

    /**
     * 更新指定订单
     *
     * @param order order
     * @return resultBean
     */
    @RequestMapping(value = "/update",method = {RequestMethod.POST,RequestMethod.GET})
    public ResultBean update(Order order) throws InvokeException {
        ValidateUtils.assertCondition(!StringUtils.isEmpty(order.getId()),"id必填");
        orderService.changeStatus(order.getId(), String.valueOf(order.getStatus()));
        return ResultBean.successResult(OPERATOR_SUCCESS);
    }
}
